public class QuickSort {
    public static int[] quickSort(int[] arr, int start, int end) {
        int left = start;
        int right = end;
        //选择基准元素为第一个元素
        int tmp = arr[left];
        while (left < right) {
            while (left < right && arr[left] < tmp) {//左边元素大于基准数指针停下
                left++;
            }
            while (left < right && arr[right] > tmp) {//右边元素小基准数指针停下
                right--;
            }
            if (arr[left] == arr[right]) {//都等于基准数时left向后走，防止循环
                left++;
            } else { //交换
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
            }
        }
        if (left - 1 > start) {
            quickSort(arr, start, left - 1); //左边进行快排
        }
        if (right + 1 < end) {
            quickSort(arr, right + 1, end); //右边进行快排
        }
        return arr;
    }
    public static void main(String[] args) {
        int[] arr = {2, 5, 6, 2, 3, 4, 8, 9};
        quickSort(arr,0,arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
